package six.com.crawler.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimplePoolSizeCaculatorImpl extends PoolSizeCalculator {

	final static Logger LOG = LoggerFactory.getLogger(SimplePoolSizeCaculatorImpl.class);
	
	@Override
	protected Runnable creatTask() {
		return new AsyncIOTask();
	}

	@Override
	protected BlockingQueue<Runnable> createWorkQueue() {
		return new LinkedBlockingQueue<Runnable>(1000);
	}

	@Override
	protected long getCurrentThreadCPUTime() {
		return ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
	}

	public static void main(String[] args) {
		PoolSizeCalculator poolSizeCalculator = new SimplePoolSizeCaculatorImpl();
		poolSizeCalculator.calculateBoundaries(new BigDecimal(1.0), new BigDecimal(100000));
	}

	/**
	 * 自定义的异步IO任务
	 * 
	 * @author Will
	 *
	 */
	class AsyncIOTask implements Runnable {

		@Override
		public void run() {
			HttpURLConnection connection = null;
			BufferedReader reader = null;
			try {
				String getURL = "http://baidu.com";
				URL getUrl = new URL(getURL);

				connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect();
				reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				String line = null;
				while ((line = reader.readLine()) != null) {
				LOG.info(line);
				}
			} catch (IOException e) {

			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (Exception e) {

					}
				}
				if(null!=connection){
					connection.disconnect();
				}
			}

		}

	}
}
